/**
 * Created by Administrator on 2017/6/26.
 */
(function () {
    start();
    function start() {
        if (getQueryString("_from") === "first") {
            first();
        }
        if (getQueryString("_from") === "baidu") {
            baidu();
        }
    }

    function sleep(time) {
        var delay = time * 1000
        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURI(r[2]));
        return null;
    }

    function runStep(array) {
        setTimeout(function () {
            console.log('即将开始执行步骤对象............');
            next(array,0)
        }, 2000)

    }

    function next(array,num) {
        array[num]();
        if (num + 1 <array.length) {
            setTimeout(function () {
                next(array,num+1)
            }, 1000)
        }

    }

    function first() {
        console.log("进入first函数")
        location.href = "http://www.baidu.com?_from=baidu";
    }

    function baidu() {
        var step1 = function () {
            console.log("进入step1()函数")
            document.getElementById("kw").value = "qq"
        }
        var step2 = function () {
            console.log("进入step2()函数")
            var objects = document.querySelectorAll("#container #content_left .result h3")
            console.info(objects)
            for (var n = 0; n < objects.length; n++) {
                console.info(objects[n])
            }
        }
        var step3 = function () {
            console.log("进入step3()函数")
            var current = document.querySelector("#container #content_left .result h3")
            console.info(current.querySelector("a").href)
            current.querySelector("a").click();
            //alert("click");
        }
        var steps = new Array();
        steps.push(step1, step2,step3)
        runStep(steps)
    }

})()



